#include "raylib.h"
#include <cmath>
#include <string>

// 坐标系转换函数：将数学坐标转换为屏幕坐标
Vector2 MathToScreen(float x, float y, int centerX, int centerY, float scale) {
    return (Vector2){ centerX + x * scale, centerY - y * scale };
}

// 添加CodepointRemoveDuplicates函数的完整实现
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
{
    int codepointsNoDupsCount = codepointCount;
    int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
    memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));

    // Remove duplicates
    for (int i = 0; i < codepointsNoDupsCount; i++)
    {
        for (int j = i + 1; j < codepointsNoDupsCount; j++)
        {
            if (codepointsNoDups[i] == codepointsNoDups[j])
            {
                for (int k = j; k < codepointsNoDupsCount; k++) codepointsNoDups[k] = codepointsNoDups[k + 1];

                codepointsNoDupsCount--;
                j--;
            }
        }
    }

    *codepointsResultCount = codepointsNoDupsCount;
    return codepointsNoDups;
}



int main(void)
{
    // 初始化窗口和基本参数
    const int screenWidth = 800;
    const int screenHeight = 600;
    InitWindow(screenWidth, screenHeight, "y = ax + b 函数图像");
    
    // 加载中文字体
    const char* text = "使用方向键调整参数：↑↓改变斜率a，←→改变截距b";
    int codepointCount = 0;
    int* codepoints = LoadCodepoints(text, &codepointCount);
    int codepointsNoDupsCount = 0;
    int* codepointsNoDups = CodepointRemoveDuplicates(codepoints, codepointCount, &codepointsNoDupsCount);
    UnloadCodepoints(codepoints);
    
    // 加载字体，设置字号为20
    Font font = LoadFontEx("resources/msyh.ttf", 20, codepointsNoDups, codepointsNoDupsCount);
    free(codepointsNoDups);
    SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
    
    SetTargetFPS(60);

    // 坐标系参数
    const int centerX = screenWidth / 2;   // 原点x坐标
    const int centerY = screenHeight / 2;  // 原点y坐标
    const float scale = 50.0f;            // 坐标系缩放比例
    
    // 函数参数 y = ax + b
    float a = 1.0f;    // 斜率
    float b = 0.0f;    // y轴截距
    
    while (!WindowShouldClose())
    {
        // 更新
        // 使用方向键上下调整斜率a
        if (IsKeyDown(KEY_UP)) a += 0.1f;
        if (IsKeyDown(KEY_DOWN)) a -= 0.1f;
        // 使用方向键左右调整截距b
        if (IsKeyDown(KEY_RIGHT)) b += 0.1f;
        if (IsKeyDown(KEY_LEFT)) b -= 0.1f;

        // 绘制
        BeginDrawing();
        ClearBackground(RAYWHITE);

        // 绘制坐标轴
        DrawLine(0, centerY, screenWidth, centerY, BLACK);  // x轴
        DrawLine(centerX, 0, centerX, screenHeight, BLACK); // y轴
        
        // 绘制刻度
        for (int i = -10; i <= 10; i++) {
            // x轴刻度
            Vector2 tickX = MathToScreen(i, 0, centerX, centerY, scale);
            DrawLine(tickX.x, tickX.y - 5, tickX.x, tickX.y + 5, BLACK);
            
            // y轴刻度
            Vector2 tickY = MathToScreen(0, i, centerX, centerY, scale);
            DrawLine(tickY.x - 5, tickY.y, tickY.x + 5, tickY.y, BLACK);
        }

        // 绘制函数 y = ax + b
        for (float x = -10; x <= 10; x += 0.05f) {    // 减小步长，使点更密集
            float y = a * x + b;
            Vector2 point = MathToScreen(x, y, centerX, centerY, scale);
            DrawCircle(point.x, point.y, 1.5f, RED);   // 调整圆点大小为1.5像素
        }

        // 显示当前函数表达式和操作说明
        const char* expression;
        if (b >= 0) {
            expression = TextFormat("y = %.1fx + %.1f", a, b);
        } else {
            expression = TextFormat("y = %.1fx - %.1f", a, -b);  // b为负数时，显示减号并取b的绝对值
        }
        DrawText(expression, 10, 10, 20, DARKGRAY);
        DrawTextEx(font, text, (Vector2){10, 40}, 20, 1, DARKGRAY);

        EndDrawing();
    }

    // 清理资源
    UnloadFont(font);
    CloseWindow();
    return 0;
}


